#!/bin/bash

# software distribution of enqueuers to one processor and dequeuers to the other (available only for SPARC CC compiler!!!)
distributed_mode="0"

rm -f results
rm -f tblResults

#isviewary="0 64 128 192 256 320 384"
isviewary="0"


#number of time throughput experiment is conducted 
seconds="3"

#if dedicated = "1" , each thread created is enqueuer or dequeuer during runtime, it may not changes roles
dedicated="1"

#left side is the percent of enqueuers , right side is the percent of dequeuers
#percents="0_100 25_75 50_50 75_25 100_0"
percents="50_50"

#3 modes for read_write_mode: [0,1,2], "0" for throughput (seconds is the time we measure throughput),
# 	"1" for Enqueue than Dequeue Experiment, "2" for Throttling experiment
read_write_mode="0"

#Number of items added be single enqueier if read_write_mode= "1" or "2"
add_items="1000000"

# executing the same experiment with each of the next number of threads in the list.
threads="2 4 8 16 24 32 40 48 56 64 80 96 112 128"

# each algorithm will be tested agains each different number of threads
algorithms="afcqueue ccsynchqueue hsynchqueue fcqueue msqueue basketqueue"

#tests="TIME Idle_strands Instr_cnt DC_miss L2_dmiss_ld DTLB_miss CPU_st_to_PCX MMU_ld_to_PCX"
#tests="L2_dmiss_ld"
tests="TIME"
#tests="DC_miss TIME"
#tests="TIME DC_miss DTLB_miss"

#read write delay between operations - dismiss the cache influence on the experiments
#delay=$(( $RANDOM % 64 + 1 ))	
delay=10

#file for output of results
output="TIME_SPARC"

#initial capacities for experiments, e.g. when only dequeuers are checked, we wouldn't like to test it on empty queue
capacities="1000000"
loadfactor="0.0"

#number of times to repeat the experiments
rep="1"
count=0

for test in $tests; do

rm -f $test

rm -f $output

for algorithm in $algorithms; do

for isview in $isviewary; do

for percent in $percents; do

for capacity in $capacities; do

for thread in $threads; do

	count=$(($count + 1))
	if [ $percent == "0_100" ]; then
		perf=0
	fi
	if [ $percent == "25_75" ]; then
		perf=0.25
	fi
	if [ $percent == "50_50" ]; then
		perf=0.5
	fi
	if [ $percent == "75_25" ]; then
		perf=0.75
	fi
	if [ $percent == "100_0" ]; then
		perf=1
	fi

        line="$algorithm 1 non 0 non 0 non 0 $count $thread $percent $loadfactor $capacity $seconds $dedicated $isview $delay $read_write_mode $add_items $distributed_mode"
        line=`echo $line | sed 's/_/ /g'`
        echo "$line" 1>&2
        #echo -n "$line" >> $test

        for rep1 in $rep; do

           if [ $test == "TIME" ]
             then
			  printf "5,%d,%s,1," $thread $perf >> $output
              taskset -c 0 ./test_intel64 $line  >> $output
              printf ",0,0,0,0,0\n" >> $output
           fi

           if [ $test != "TIME" ]
             then
              cputrack -T 9999 -e -f -c $test ./test_intel64 $line | awk '($4 == "lwp_exit") {printf " %d\n",$5}' | awk '{tot=tot+$1} END {printf "%d", tot}' >> $test
           fi

           cp $output results
           echo -n " " >> results
           echo $output >> results
        done;

done; done; done; done; done; done;

cat results | grep lwp_exit | awk '($3 > 18) && ($5 > 0) {print $3, $5, $6}' > cache_miss.txt
